package cn.wwcyc.controller;

import cn.wwcyc.domain.R;
import cn.wwcyc.domain.TjDev;
import cn.wwcyc.domain.TjDevtype;
import cn.wwcyc.domain.TjSpecialtype;
import cn.wwcyc.mapper.TjDevMapper;
import cn.wwcyc.mapper.TjDevtypeMapper;
import cn.wwcyc.mapper.TjSpecialtypeMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/devtype")
public class DEVTYPEController {
    @Autowired
    TjDevtypeMapper tjDevtypeMapper;

    @RequestMapping("/getAll")
    public R<List<TjDevtype>> getAll() {
        List<TjDevtype> list = tjDevtypeMapper.selectList(null);
        return R.success(list);
    }

    @RequestMapping("/getById")
    public R<TjDevtype> getById(Long id) {
        TjDevtype devtype = tjDevtypeMapper.selectById(id);
        return R.success(devtype);
    }

    //此分页函数中的返回对象中不含有单位及类型对象数据
    @RequestMapping("/getpage")
    public R<IPage<TjDevtype>> getPage1(
            @RequestParam Integer pagenum,
            @RequestParam Integer pagesize,
            @RequestParam(required = false) String name
    ) {
        LambdaQueryWrapper<TjDevtype> qw = new LambdaQueryWrapper();
        if (!StringUtils.isBlank(name))
            qw.like(TjDevtype::getName, name);
        IPage<TjDevtype> page = new Page<>(pagenum, pagesize);
        page = tjDevtypeMapper.selectPage(page, qw);
        return R.success(page);
    }

    @PostMapping("/save")
    public R save(@RequestBody TjDevtype specialtype) {
        tjDevtypeMapper.insert(specialtype);
        return R.success();
    }

    @PostMapping("/edit")
    public R edit(@RequestBody TjDevtype tjDevtype) {
        tjDevtypeMapper.updateById(tjDevtype);
        return R.success();
    }

    //
    @DeleteMapping
    public R del(Integer id) {
        tjDevtypeMapper.deleteById(id);
        return R.success();
    }

    @PostMapping("delbatch")
    public R delbatch(@RequestBody List<TjDevtype> list) {
        for (TjDevtype devtype : list) {
            Long id = devtype.getId();
            tjDevtypeMapper.deleteById(id);
        }
        return R.success();
    }
}
